package 力扣.堆.优先队列;

import org.omg.CORBA.INTERNAL;

import java.util.Comparator;
import java.util.PriorityQueue;

class Person{
    private Integer id;
    private Integer age;
    private Integer tall;

    public Person(Integer id, Integer age, Integer tall) {
        this.id = id;
        this.age = age;
        this.tall = tall;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getTall() {
        return tall;
    }

    public void setTall(Integer tall) {
        this.tall = tall;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", age=" + age +
                ", tall=" + tall +
                '}';
    }
}
public class 优先队列 {
    /**
     * 常用方法：
     *
     * peek()//返回队首元素
     * poll()//返回队首元素，队首元素出队列
     * add()//添加元素 若队列满了，则抛出异常
     * offer() //添加元素 若队列满了，则返回false
     * size()//返回队列元素个数
     * isEmpty()//判断队列是否为空，为空返回true,不空返回false
     *
     */
    public static void main(String[] args) {
//        PriorityQueue<Integer> pq = new PriorityQueue<>();默认是小根堆
        PriorityQueue pq = new PriorityQueue<Integer>(new Comparator<Integer>() {//通过重写抽象类Comparator中的compare方法可以更改比较方式
            @Override
            public int compare(Integer o1, Integer o2) {
                //return  o1 - o2;// o1 减去 o2 ：则是小根堆
                return  o2 - o1;//  o2 减去 o1：则是大根堆
            }
        });
        pq.offer(5);
        pq.offer(4);
        pq.offer(-1);
        pq.offer(8);
        System.out.println(pq);

        //根据优先队列得到排序后的数字
        for (int i = 0; !pq.isEmpty() ; i++) {
            System.out.print(pq.poll()+" ");//得到队首元素，并移除
        }
        System.out.println("*******************");
        System.out.println("*******************");
        comparatorTest();
    }
    public static void comparatorTest(){
        Person xiaoming = new Person(7, 18, 100);
        Person xiaoHong = new Person(2, 16, 100);
        Person xiaoWei = new Person(2, 15, 100);
        PriorityQueue<Person> pq = new PriorityQueue<>(new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                if (p1.getId() != p2.getId()){
                    return p1.getId() - p2.getId();
                }else if (p1.getAge() != p2.getAge()){
                    return p1.getAge() - p2.getAge();
                }else
                    return p1.getTall() - p2.getTall();
//                return p1.getId() == p2.getId() ? (p1.getAge() == p2.getAge() ? p1.getTall() - p2.getTall() : p1.getAge() - p2.getAge()) : p1.getId() - p2.getId();
            }
        });
        pq.offer(xiaoming);
        pq.offer(xiaoHong);
        pq.offer(xiaoWei);
        System.out.println(pq.toString());//生成一个自定义小根堆 [Person{id=2, age=15, tall=100}, Person{id=7, age=18, tall=100}, Person{id=2, age=16, tall=100}]
    }

}
